package com.light.hessian.md5.util;

import com.github.moaxcp.recmd5.MD5MessageDigest;
import com.github.moaxcp.recmd5.test.Test;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MD5Util {
    public static String digest(String str) {
        MessageDigest expected = null;
        MD5MessageDigest md5Digest = null;
        try {
            expected = MessageDigest.getInstance("md5");
            md5Digest = new MD5MessageDigest();
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, (String)null, e);
        }
        expected.update(str.getBytes());
        String s = (new BigInteger(1, expected.digest())).toString(16);
        if(s.length() == 31) {
            s = "0" + s;
        }
        md5Digest.digest(str.getBytes());
        return  md5Digest.getState().toString();
    }
}
